<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
<!-- qdoc-manual-markupcmds.qdoc -->
  <title>Including Code Inline | QDoc Manual 5.14.2</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td ><a href="../qtdoc/index.html">Qt 5.14</a></td><td ><a href="qdoc-index.html">QDoc Manual</a></td><td >Including Code Inline</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right"><a href="qdoc-index.html">Qt 5.14.2 Reference Documentation</a></td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
  <link rel="prev" href="05-qdoc-commands-documentstructure.html" />
  <link rel="next" href="07-0-qdoc-commands-includingexternalcode.html" />
<p class="naviNextPrevious headerNavi">
<a class="prevPage" href="05-qdoc-commands-documentstructure.html">Document Structure</a>
<span class="naviSeparator">  &#9702;  </span>
<a class="nextPage" href="07-0-qdoc-commands-includingexternalcode.html">Including External Code</a>
</p><p/>
<div class="sidebar">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#code">\code</a></li>
<li class="level2"><a href="#code-snippet-parameters">Code snippet parameters</a></li>
<li class="level2"><a href="#including-code-from-external-files">Including code from external files</a></li>
<li class="level1"><a href="#badcode">\badcode</a></li>
<li class="level1"><a href="#newcode">\newcode</a></li>
<li class="level1"><a href="#oldcode">\oldcode</a></li>
<li class="level1"><a href="#qml">\qml</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">Including Code Inline</h1>
<span class="subtitle"></span>
<!-- $$$06-qdoc-commands-includecodeinline.html-description -->
<div class="descr"> <a name="details"></a>
<p>The following commands are used to render source code without formatting. The source code begins on a new line, rendered in the code.</p>
<p><b>Note: </b>Although most of these commands are for rendering C++ code, the <a href="07-0-qdoc-commands-includingexternalcode.html#snippet-command">\snippet</a> and <a href="07-0-qdoc-commands-includingexternalcode.html#codeline-command">\codeline</a> commands are preferred over the others. These commands allow equivalent code snippets for other Qt language bindings to be substituted for the C++ snippets in the documentation.</p><a name="code-command"></a><a name="code"></a>
<h2 id="code">\code</h2>
<p>The \code and \endcode commands enclose a snippet of source code.</p>
<p><b>Note: </b>The <a href="04-qdoc-commands-textmarkup.html#c-command">\c</a> command can be used for short code fragments within a sentence. The \code command is for longer code snippets. It renders the code verbatim in a separate paragraph in a html &lt;pre&gt; element, and parses the enclosed snippet, creating links to any known types in the code.</p><p>For documenting command-line instructions, shell scripts, or any content that is not in a Qt language recognized by QDoc, use <a href="06-qdoc-commands-includecodeinline.html#badcode-command">\badcode</a> instead.</p>
<p>When processing any of the \code, <a href="06-qdoc-commands-includecodeinline.html#newcode-command">\newcode</a> or <a href="06-qdoc-commands-includecodeinline.html#oldcode-command">\oldcode</a> commands, QDoc removes all indentation that is common for the verbatim code blocks within a <code>/</code><code>*!</code> ..&#x2e; <code>*</code><code>/</code> comment before it adds the standard indentation.</p>
<p><b>Note: </b>This doesn't apply to externally quoted code using the <a href="07-0-qdoc-commands-includingexternalcode.html#quotefromfile-command">\quotefromfile</a> or <a href="07-0-qdoc-commands-includingexternalcode.html#quotefile-command">\quotefile</a> command.</p><pre class="cpp">

  <span class="operator">/</span> <span class="operator">*</span><span class="operator">!</span>
      \code
          <span class="preprocessor">#include &lt;QApplication&gt;</span>
          <span class="preprocessor">#include &lt;QPushButton&gt;</span>

          <span class="type">int</span> main(<span class="type">int</span> argc<span class="operator">,</span> <span class="type">char</span> <span class="operator">*</span>argv<span class="operator">[</span><span class="operator">]</span>)
          {
              <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>
          }
      \ endcode
  <span class="operator">*</span> <span class="operator">/</span>

</pre>
<p>QDoc renders this as:</p>
<pre class="cpp">

  <span class="preprocessor">#include &lt;QApplication&gt;</span>
  <span class="preprocessor">#include &lt;QPushButton&gt;</span>

  <span class="type">int</span> main(<span class="type">int</span> argc<span class="operator">,</span> <span class="type">char</span> <span class="operator">*</span>argv<span class="operator">[</span><span class="operator">]</span>)
  {
      <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>
  }

</pre>
<p>Other QDoc commands are disabled within \code..&#x2e; \endcode, and the special character '\' is accepted and rendered like the rest of the code, unless it is followed by a digit and parameters were passed to \code.</p>
<a name="code-snippet-parameters"></a>
<h3 id="code-snippet-parameters">Code snippet parameters</h3>
<p>Since QDoc version 5.12, \code command accepts also optional parameters. Parameters are useful for injecting simple strings into the code snippet. To inject a string to a specific location in the snippet, add a backslash followed by a digit (1..8). The digits correspond with the order of the argument list, where arguments are separated by spaces.</p>
<p>For example:</p>
<pre class="cpp">

  <span class="operator">/</span> <span class="operator">*</span><span class="operator">!</span>
  \code <span class="operator">*</span> hello
  <span class="operator">/</span>\<span class="number">1</span> \<span class="number">2</span> \<span class="number">1</span><span class="operator">/</span>
  \ endcode
  <span class="operator">*</span> <span class="operator">/</span>

</pre>
<p>For the above snippet, QDoc renders the word <i>hello</i> enclosed in a C-style comment.</p>
<a name="including-code-from-external-files"></a>
<h3 id="including-code-from-external-files">Including code from external files</h3>
<p>To include code snippets from an external file, use the <a href="07-0-qdoc-commands-includingexternalcode.html#snippet-command">\snippet</a> and <a href="07-0-qdoc-commands-includingexternalcode.html#codeline-command">\codeline</a> commands.</p>
<p>See also <a href="04-qdoc-commands-textmarkup.html#c-command">\c</a>, <a href="06-qdoc-commands-includecodeinline.html#badcode-command">\badcode</a>, <a href="07-0-qdoc-commands-includingexternalcode.html#quotefromfile-command">\quotefromfile</a>, <a href="06-qdoc-commands-includecodeinline.html#newcode-command">\newcode</a>, and <a href="06-qdoc-commands-includecodeinline.html#oldcode-command">\oldcode</a>.</p>
<a name="badcode-command"></a><a name="badcode"></a>
<h2 id="badcode">\badcode</h2>
<p>Similar to <a href="06-qdoc-commands-includecodeinline.html#code-command">\code</a>, \badcode and \endcode commands enclose content that is rendered verbatim in a separate paragraph, but no parsing or automatic link creation is performed. Instead, the content is treated as plain text.</p>
<p>Substitute \code with this command when documenting command-line instructions, shell scripts or any other content that is not in a Qt language, but should still be styled similarly to a \code paragraph.</p>
<p>Like \code, \badcode accepts also optional parameters.</p>
<a name="newcode-command"></a><a name="newcode"></a>
<h2 id="newcode">\newcode</h2>
<p>The \newcode, \oldcode, and \endcode commands enable you to show how to port a snippet of code to a new version of an API.</p>
<p>The \newcode command and its companion the \oldcode command are a convenience combination of the <a href="06-qdoc-commands-includecodeinline.html#code-command">\code</a> commands: this combination provides a text relating the two code snippets to each other.</p>
<p>The \newcode command requires a preceding \oldcode statement.</p>
<p>Like the <a href="06-qdoc-commands-includecodeinline.html#code-command">\code</a> command, the \newcode command renders its code on a new line in the documentation using a monospace font and the standard indentation.</p>
<pre class="cpp">

  <span class="operator">/</span> <span class="operator">*</span><span class="operator">!</span>
      \oldcode
          <span class="keyword">if</span> (printer<span class="operator">-</span><span class="operator">&gt;</span>setup(parent))
              <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>
      \newcode
          <span class="type"><a href="../qtprintsupport/qprintdialog.html">QPrintDialog</a></span> dialog(printer<span class="operator">,</span> parent);
              <span class="keyword">if</span> (dialog<span class="operator">.</span>exec())
                  <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>
      \ endcode
  <span class="operator">*</span> <span class="operator">/</span>

</pre>
<p>QDoc renders this as:</p>
<blockquote><p>For example, if you have code like</p>
<pre class="cpp plain">

  if (printer-&gt;setup(parent))
      ..&#x2e;

</pre>
<p>you can rewrite it as</p>
<pre class="cpp">

  <span class="type"><a href="../qtprintsupport/qprintdialog.html">QPrintDialog</a></span> dialog(printer<span class="operator">,</span> parent);
      <span class="keyword">if</span> (dialog<span class="operator">.</span>exec())
          <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>

</pre>
</blockquote>
<p>Other QDoc commands are disabled within \oldcode ..&#x2e; \endcode, and the '\' character doesn't need to be escaped.</p>
<a name="oldcode-command"></a><a name="oldcode"></a>
<h2 id="oldcode">\oldcode</h2>
<p>The \oldcode command requires a corresponding \newcode statement; otherwise QDoc fails to parse the command and emits a warning.</p>
<p>See also <a href="06-qdoc-commands-includecodeinline.html#newcode-command">\newcode</a>.</p>
<a name="qml-command"></a><a name="qml"></a>
<h2 id="qml">\qml</h2>
<p>The \qml and \endqml commands enclose a snippet of QML source code.</p>
<pre class="cpp">

  <span class="operator">/</span> <span class="operator">*</span><span class="operator">!</span>
      \qml
          import <span class="type"><a href="../qtquick/qtquick-module.html">QtQuick</a></span> <span class="number">2.0</span>

          Row {
              Rectangle {
                  width: <span class="number">100</span>; height: <span class="number">100</span>
                  color: <span class="string">&quot;blue&quot;</span>
                  transform: Translate { y: <span class="number">20</span> }
              }
              Rectangle {
                  width: <span class="number">100</span>; height: <span class="number">100</span>
                  color: <span class="string">&quot;red&quot;</span>
                  transform: Translate { y: <span class="operator">-</span><span class="number">20</span> }
              }
          }
      \endqml
  <span class="operator">*</span> <span class="operator">/</span>

</pre>
<p>QDoc renders this as:</p>
<pre class="qml">

  import QtQuick 2.0

  <span class="type"><a href="../qtquick/qml-qtquick-row.html">Row</a></span> {
      <span class="type"><a href="../qtquick/qml-qtquick-rectangle.html">Rectangle</a></span> {
          <span class="name">width</span>: <span class="number">100</span>; <span class="name">height</span>: <span class="number">100</span>
          <span class="name">color</span>: <span class="string">&quot;blue&quot;</span>
          <span class="name">transform</span>: <span class="name">Translate</span> { <span class="name">y</span>: <span class="number">20</span> }
      }
      <span class="type"><a href="../qtquick/qml-qtquick-rectangle.html">Rectangle</a></span> {
          <span class="name">width</span>: <span class="number">100</span>; <span class="name">height</span>: <span class="number">100</span>
          <span class="name">color</span>: <span class="string">&quot;red&quot;</span>
          <span class="name">transform</span>: <span class="name">Translate</span> { <span class="name">y</span>: -<span class="number">20</span> }
      }
  }

</pre>
<p>Like the <a href="06-qdoc-commands-includecodeinline.html#code-command">\code</a> command, \qml accepts optional parameters.</p>
</div>
<!-- @@@06-qdoc-commands-includecodeinline.html -->
<p class="naviNextPrevious footerNavi">
<a class="prevPage" href="05-qdoc-commands-documentstructure.html">Document Structure</a>
<span class="naviSeparator">  &#9702;  </span>
<a class="nextPage" href="07-0-qdoc-commands-includingexternalcode.html">Including External Code</a>
</p>
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2020 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br/>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br/>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>
